package com.caipiao.service.impl;



import com.caipiao.mapper.SMobileCodeMapper;
import com.caipiao.pojo.ObjectResult;
import com.caipiao.pojo.SMobileCode;
import com.caipiao.pojo.XMLBean;
import com.caipiao.service.MobileCodeService;
import com.caipiao.util.DateUtil;
import com.caipiao.util.HttpClientUtil;
import com.caipiao.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangweijian on 2018/1/15.
 */
@Service
@Transactional
public class MobileCodeServiceImpl implements MobileCodeService {

    public static final String SMS_KEY = "c04592750fa1636f79da30747259a326";

    //短信请求URL
    public static final String SMS_URL = "http://v.juhe.cn/sms/send";

    @Autowired
    private SMobileCodeMapper sMobileCodeMapper;
    @Override
    public ObjectResult  checkMobileCode(XMLBean xmlBean) {
        SMobileCode mobileCode = sMobileCodeMapper.selectCodeByMobile(xmlBean.getMobile());

        if(mobileCode.getCode()==null || !mobileCode.getCode().equals(xmlBean.getCode())){
            return ObjectResult.build("0","验证码错误");
        }
        if(System.currentTimeMillis()-mobileCode.getSendTime()>5*60*1000){
            return ObjectResult.build("0","验证码失效");
        }

        return ObjectResult.build("1","正确");
    }

    @Override
    public void insertMobileCode(SMobileCode mobileCode) {
        sMobileCodeMapper.insertSelective(mobileCode);
    }

    @Override
    public ObjectResult sms_code(XMLBean xmlBean, String ip) throws Exception {
        Map<String,Object> map = new HashMap<String,Object>();
        String code = RandomUtil.getRandom(6);

        String date1 = DateUtil.formatDate();
        String date2 = DateUtil.formatDate(DateUtil.addDateDay(new Date(), 1));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        long startTime = format.parse(date1).getTime();
        long endTime = format.parse(date2).getTime();

        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("mobile",xmlBean.getMobile());
        Integer count = sMobileCodeMapper.selectMobileCodeCount(map);
        if(count>10){
            return  ObjectResult.build("0","短信发送超过限制");
        }
        map.clear();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("ip",ip);
        count = sMobileCodeMapper.selectMobileCodeCount(map);
        if(count>20){
            return  ObjectResult.build("0","短信发送超过限制");
        }

        //添加纪录
        SMobileCode mobileCode = new SMobileCode();
        mobileCode.setMobile(xmlBean.getMobile());
        mobileCode.setCode(code);
        mobileCode.setSendTime(System.currentTimeMillis());
        mobileCode.setIp(ip);
        sMobileCodeMapper.insertSelective(mobileCode);
        //发送短信验证码
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("mobile", xmlBean.getMobile());
        hashMap.put("tpl_id", "130101");
        hashMap.put("key", SMS_KEY);
        hashMap.put("tpl_value", "#code#="+code);
        ObjectResult objectResult2 = HttpClientUtil.verificationOfBankCard(SMS_URL, hashMap);
        return  ObjectResult.build("1","发送成功");
    }
}
